package cn.edu.nju.sharing;

import java.util.Map;

import cn.edu.nju.R;
import cn.edu.nju.newsFeed.NewsFeedActivity;
import cn.edu.nju.notification.NotificationActivity;
import cn.edu.nju.notification.NotificationProvider;
import cn.edu.nju.setting.SettingActivity;
import cn.edu.nju.storage.StoreDataProvider;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class StartUpActivity extends Activity {
    private StoreDataProvider provider;
    private StartUpProvider startUpProvider;
    private boolean hasLogin = false;
    private TextView userNameText;
    
    public static double latitude = 0;
    public static double longitude = 0;
    
    
	@Override
	public void onCreate(Bundle savedInstanceState){
		LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		LocationListener locationListener = new LocationListener() {
			
			public void onStatusChanged(String provider, int status, Bundle extras) {
				
			}
			
			public void onProviderEnabled(String provider) {
				
			}
			
			public void onProviderDisabled(String provider) {
				
			}
			
			public void onLocationChanged(Location location) {
				latitude  = location.getLatitude();
				longitude = location.getLongitude();
			}
		};
		
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5, 5.0f, locationListener);
		
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
        
		provider = StoreDataProvider.getInstance(this);
		startUpProvider = StartUpProvider.getInstance(this);
		
		if(provider.getUserName() == null){
			// user doesn't login, switch to the login activity
			hasLogin = false;
		}
		else{
			//verify the local username and password
			Map<String, String> userInfo = startUpProvider.getRecentLoginUser();
			if(startUpProvider.login(userInfo.get("name"), userInfo.get("password")) == -1){
				hasLogin = false;
			}
			else{
				hasLogin = true;
			}
		}
		
		initMainPage();		
	}
	
	@Override
	public void onResume(){
		super.onResume();
		if(provider.getUserName() == null){
			hasLogin = false;
		}
		else{
			hasLogin = true;
			userNameText.setText(provider.getUserName());
			TextView notificationNumberText = (TextView) findViewById(R.id.notification_number_text);
			notificationNumberText.setText("" + NotificationProvider.getInstance(this).getNotificationNumber());
		}
		
		if(!hasLogin){
			// start login activity
			Intent intent = new Intent(StartUpActivity.this, LoginActivity.class);
			startActivity(intent);
		}
	}
	
	private void initMainPage(){
		setContentView(R.layout.main);
		
		// init widget
		userNameText = (TextView)findViewById(R.id.user_name_text);
		userNameText.setText(StoreDataProvider.getInstance(this).getUserName());
		
		initPageSelectionButtons();
		initLogoutButton();
	}
	
	private void initPageSelectionButtons(){
		Button newsFeedButton = (Button)findViewById(R.id.news_feed_page);
		newsFeedButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				Intent intent = new Intent(StartUpActivity.this, NewsFeedActivity.class);
				startActivity(intent);
			}
		});
		
		Button settingButton = (Button)findViewById(R.id.setting_page);
		settingButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(StartUpActivity.this, SettingActivity.class);
				startActivity(intent);
			}
		});
		
		
		TextView notificationNumberText = (TextView) findViewById(R.id.notification_number_text);
		notificationNumberText.setText("" + NotificationProvider.getInstance(this).getNotificationNumber());
		Button notificationButton = (Button) findViewById(R.id.notification_page);
		notificationButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(StartUpActivity.this, NotificationActivity.class);
				startActivity(intent);
			}
		});
	}
	
	private void initLogoutButton(){
		Button logoutButton = (Button)findViewById(R.id.logout);
		logoutButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				startUpProvider.clearCurrentUser();
				Intent intent = new Intent(StartUpActivity.this, LoginActivity.class);
				startActivity(intent);
			}
		});
	}
}